package com.ch12.config;

import com.ch12.anno.MyAnno;
import com.ch12.service.UserService;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName AnnoImportSelector
 * @Description 自定义导入选择器;如果类上标注了@MyAnno注解,就将其纳入Spring容器管理
 * @Author Kx
 * @Date 2025/5/4 17:45
 * @Version 1.0
 */
public class AnnoImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        // 定义一个集合
        List<String> list = new ArrayList<>();

        // 判断类上是否标注了@MyAnno注解
        if (UserService.class.isAnnotationPresent(MyAnno.class)) {
            // 添加UserService类(全类名路径)
            list.add(UserService.class.getName());
        }

        // 把集合转成数组
        return StringUtils.toStringArray(list);
    }
}
